package com.shuai.blog.service;

import com.shuai.blog.entity.ArticleInfo;
import com.shuai.blog.mapper.ArticleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@Service
public class ArticleService {
    @Resource
    private ArticleMapper articleMapper;
    private final Timer timer = new Timer();

    public Integer getCountByUserId(Integer userId) {
        return articleMapper.getCountByUserId(userId);
    }

    public List<ArticleInfo> getBlogList(Integer userId) {
        return articleMapper.getBlogList(userId);
    }

    public Integer deleteArt(Integer userId, Integer artId) {
        return articleMapper.deleteArt(userId, artId);
    }

    public ArticleInfo getArtDetail(Integer artId) {
        return articleMapper.getArtDetail(artId);
    }

    public int incrementVisits(Integer artId) {
        return articleMapper.incrementVisits(artId);
    }

    /**
     * 完成新增文章的操作
     */
    public int addBlog(ArticleInfo articleInfo) {
        return articleMapper.addBlog(articleInfo);
    }

    /**
     * 完成更新文章的操作
     */
    public int updateBlog(ArticleInfo articleInfo) {
        return articleMapper.updateBlog(articleInfo);
    }

    /**
     * 完成分页查询的功能
     */
    public List<ArticleInfo> getPageList(Integer pageSize, Integer offset) {
        return articleMapper.getPageList(pageSize, offset);
    }

    /**
     * 查询数据库中文章的总数量
     */
    public int getCount() {
        return articleMapper.getCount();
    }

    /**
     * 新增草稿
     */
    public int addDraft(ArticleInfo articleInfo) {
        return articleMapper.addDraft(articleInfo);
    }


    /**
     * 更新草稿
     */
    public int updateDraft(ArticleInfo articleInfo) {
        return articleMapper.updateDraft(articleInfo);
    }

    /**
     * 定时发布
     */
    public int onTimePush(ArticleInfo articleInfo) {
        int affect = articleMapper.onTimePush(articleInfo);
        if (affect == 1) {
            //创建定时器，定时扫描数据库中插入的博客的定时发布时间，到时间后就发布
            Duration delay = Duration.between(articleInfo.getCreateTime(), LocalDateTime.now());
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    int artId = articleInfo.getArtId();
                    articleMapper.onTimePushing(artId);
                }
            }, Math.abs(delay.toMillis()));
        }
        return affect;
    }

    public int deleteDraftBeforePush(ArticleInfo articleInfo) {
        return articleMapper.deleteDraftBeforePush(articleInfo);
    }

    /**
     * 增加点赞数并返回增加后的点赞数
     */
    public int addLikeNum(Integer artId) {
        articleMapper.addLikeNum(artId);
        return articleMapper.getLikeNum(artId);
    }


    /**
     * 减少点赞数并返回减少后的点赞数
     */
    public int subLikeNum(Integer artId) {
        articleMapper.subLikeNum(artId);
        return articleMapper.getLikeNum(artId);
    }
}
